package cn.inbot.shopadmin.api;

import android.os.Build;

import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.inbot.shopadmin.app.AppConfig;
import cn.inbot.lib.log.PBLog;
import cn.inbot.lib.network.PBApiFactory;
import cn.inbot.lib.network.interceptor.AutographInterceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by shisenkun on 2021/5/10.
 */
public class DemoApiFactory extends PBApiFactory {

    public Map<String, String> autographParamsMap = new HashMap();
    public OkHttpClient.Builder demoOkHttpClientBuilder;
    public Retrofit.Builder demoRetrofitBuilder;
    private static DemoApiFactory sInstance = new DemoApiFactory();
    private DemoApi demoApi;

    public static DemoApiFactory getInstance() {
        return sInstance;
    }

    @NotNull
    @Override
    public OkHttpClient.Builder customizedOkHttpClientBuilder(@NotNull OkHttpClient.Builder builder) {
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                PBLog.INSTANCE.d("OkHttp====Message:" + message);
            }
        });

        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        autographParamsMap.put("appkey", AppConfig.APP_KEY);
        autographParamsMap.put("apptype", AppConfig.APP_TYPE);
        autographParamsMap.put("appversion", AppConfig.APP_VERSION);
        autographParamsMap.put("apptoken", AppConfig.APP_TOKEN);
        autographParamsMap.put("osversion", Build.VERSION.SDK_INT + "");
        autographParamsMap.put("devicetype", Build.MODEL);

        AutographInterceptor autographInterceptor = new AutographInterceptor(autographParamsMap);

        builder.addInterceptor(loggingInterceptor)
                .addInterceptor(autographInterceptor)
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(5, TimeUnit.SECONDS);
        demoOkHttpClientBuilder = builder;
        return demoOkHttpClientBuilder;
    }

    @NotNull
    @Override
    public Retrofit.Builder customizedRetrofitBuilder(@NotNull Retrofit.Builder builder) {
        builder.addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(CoroutineCallAdapterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create());
        demoRetrofitBuilder = builder;
        return demoRetrofitBuilder;
    }

    public DemoApi getDemoApi() {
        if(demoApi == null) {
           demoApi = getApiService(DemoApi.class, AppConfig.PADBOT_SERVER_URL);
        }
        return demoApi;
    }
}
